package com.ruoyi.qd.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.qd.vo.TradeWaterAddReq;
import com.ruoyi.qd.vo.TradeWaterPageH5Req;
import com.ruoyi.qd.vo.TradeWaterPageH5Res;
import com.ruoyi.qd.vo.TradeWaterPageReq;
import com.ruoyi.qd.vo.TradeWaterPageRes;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.qd.domain.TradeWater;
import com.ruoyi.qd.service.ITradeWaterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.mybatisflex.core.paginate.Page;
import com.ruoyi.common.core.page.PageDomain;

/**
 * 交易流水Controller
 * 
 * @author tushiguang
 * @date 2024-07-24
 */
@RestController
@RequestMapping("/qd/water")
public class TradeWaterController extends BaseController {
    @Autowired
    private ITradeWaterService tradeWaterService;

    /**
     * 新增交易流水
     */
    @PreAuthorize("@ss.hasPermi('qd:water:add')")
    @Log(title = "新增交易流水", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TradeWaterAddReq tradeWaterAddReq) {
        tradeWaterService.add(tradeWaterAddReq);
        return success();
    }

    /**
     * 查询用户余额
     */
    @PreAuthorize("@ss.hasPermi('qd:water:balance')")
    @GetMapping("/balance/{userId}")
    public AjaxResult balance(@PathVariable Long userId) {
        return success(tradeWaterService.calculateBalance(userId));
    }

    /**
     * 查询交易流水列表-H5
     */
    @PreAuthorize("@ss.hasPermi('qd:water:listH5')")
    @GetMapping("/listH5")
    public TableDataInfo listH5(PageDomain pageDomain, TradeWaterPageH5Req tradeWaterPageH5Req) {
        Page<TradeWaterPageH5Res> page = tradeWaterService.selectTradeWaterPageH5(pageDomain, tradeWaterPageH5Req);
        return getFlexTable(page);
    }

    /**
     * 查询交易流水列表
     */
    @PreAuthorize("@ss.hasPermi('qd:water:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageDomain pageDomain, TradeWaterPageReq tradeWaterPageReq) {
        Page<TradeWaterPageRes> page = tradeWaterService.selectTradeWaterPage(pageDomain, tradeWaterPageReq);
        return getFlexTable(page);
    }

    /**
     * 导出交易流水列表
     */
    @PreAuthorize("@ss.hasPermi('qd:water:export')")
    @Log(title = "交易流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TradeWater tradeWater) {
        List<TradeWater> list = tradeWaterService.selectTradeWaterList(tradeWater);
        ExcelUtil<TradeWater> util = new ExcelUtil<TradeWater>(TradeWater.class);
        util.exportExcel(response, list, "交易流水数据");
    }

    /**
     * 获取交易流水详细信息
     */
    @PreAuthorize("@ss.hasPermi('qd:water:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tradeWaterService.selectTradeWaterById(id));
    }

    /**
     * 修改交易流水
     */
    @PreAuthorize("@ss.hasPermi('qd:water:edit')")
    @Log(title = "交易流水", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TradeWater tradeWater) {
        return toAjax(tradeWaterService.updateTradeWater(tradeWater));
    }

    /**
     * 删除交易流水
     */
    @PreAuthorize("@ss.hasPermi('qd:water:remove')")
    @Log(title = "交易流水", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tradeWaterService.deleteTradeWaterByIds(ids));
    }
}
